package com.kyxx.mycommonproject.net;

import android.content.Context;

import com.kyxx.mycommonproject.utils.Log;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;

import java.io.InputStream;
import java.util.List;

import cz.msebera.android.httpclient.client.params.ClientPNames;
import cz.msebera.android.httpclient.cookie.Cookie;

/**
 * @author txiuqi
 */
public class RestClient {

    private static AsyncHttpClient client = new AsyncHttpClient();

    public static void get(String url, Context context,
                           AsyncHttpResponseHandler responseHandler) {
//		if (NetworkUtil.isNetworkValidate(context)) {
//
//			try {
//				InputStream ins;
//				// 下载的证书放到项目中的assets目录中
//				ins = context.getAssets().open("licai.159.com.crt");
//				CertificateFactory cerFactory = CertificateFactory
//						.getInstance("X.509");
//				Certificate cer = cerFactory.generateCertificate(ins);
//				KeyStore keyStore = KeyStore.getInstance("PKCS12", "BC");
//				keyStore.load(null, null);
//				keyStore.setCertificateEntry("trust", cer);
//				SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore);
//				Scheme sch = new Scheme("https", socketFactory, 443);
//				client.getHttpClient().getConnectionManager()
//						.getSchemeRegistry().register(sch);
        client.get(url, null, responseHandler);
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//
//			Log.e("ok", "getRequest" + url);
//		} else {
//			Toast.makeText(context, R.string.network_error, Toast.LENGTH_SHORT)
//					.show();
//		}

    }

    public static void post(String url, RequestParams params, Context context,
                            AsyncHttpResponseHandler responseHandler) {
        // if (NetworkUtil.isNetworkValidate(context)) {
        InputStream ins;
        try {
            // 下载的证书放到项目中的assets目录中
//			ins = context.getAssets().open("licai.159.com.crt");
//			CertificateFactory cerFactory = CertificateFactory
//					.getInstance("X.509");
//			Certificate cer = cerFactory.generateCertificate(ins);
//			KeyStore keyStore = KeyStore.getInstance("PKCS12", "BC");
//			keyStore.load(null, null);
//			keyStore.setCertificateEntry("trust", cer);
//
//			SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore);
//			Scheme sch = new Scheme("https", socketFactory, 443);
//			client.getHttpClient().getConnectionManager().getSchemeRegistry()
//					.register(sch);
            PersistentCookieStore myCookieStore = new PersistentCookieStore(context);
            List<Cookie> cookieList = myCookieStore.getCookies();
            for (Cookie cookie : cookieList) {
                myCookieStore.addCookie(cookie);
            }
            client.setCookieStore(myCookieStore);
            client.getHttpClient().getParams()
                    .setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
            client.post(context, url, params, responseHandler);
            client.setTimeout(60000);
            Log.e("ok", "getRequest" + url);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        // } else {
        // Toast.makeText(context, R.string.network_error, Toast.LENGTH_SHORT)
        // .show();
        // }
    }

    public static void postHasError(String url, RequestParams params,
                                    Context context, AsyncHttpResponseHandler responseHandler) {
        InputStream ins;
        try {
            // 下载的证书放到项目中的assets目录中
//			ins = context.getAssets().open("licai.159.com.crt");
//			CertificateFactory cerFactory = CertificateFactory
//					.getInstance("X.509");
//			Certificate cer = cerFactory.generateCertificate(ins);
//			KeyStore keyStore = KeyStore.getInstance("PKCS12", "BC");
//			keyStore.load(null, null);
//			keyStore.setCertificateEntry("trust", cer);
//
//			SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore);
//			Scheme sch = new Scheme("https", socketFactory, 443);
//			client.getHttpClient().getConnectionManager().getSchemeRegistry()
//					.register(sch);
            client.post(context, url, params, responseHandler);
            client.setTimeout(60000);
            Log.e("ok", "getRequest" + url);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
}